<template>
  <hry-header title="消息通知" :showBackBtn="true">
    <div slot="content" class="personProject">
      <md-tab-bar v-model="current" :items="items" @change="changeTab" />
      <ul class="form-wrapper" v-if="current === 0">
        <md-scroll-view
          ref="scrollViewOne"
          :scrolling-x="false"
          :auto-reflow="true"
          @endReached="$_onEndReached"
          @refreshing="refresh"
        >
          <div class="empty" v-if="data.length === 0">
            <img src="@/assets/img/empty.png" alt="" />
          </div>
          <md-scroll-view-refresh
            slot="refresh"
            slot-scope="{ scrollTop, isRefreshActive, isRefreshing }"
            :scroll-top="scrollTop"
            :is-refreshing="isRefreshing"
            roller-color="#FD7400"
            :is-refresh-active="isRefreshActive"
          ></md-scroll-view-refresh>
          <li
            v-for="(item, index) in data"
            :key="index"
            @click="changeRead(item.id, index)"
          >
            <div class="content">
              <div class="message">
                <i class="iconfont iconxiaoxi"></i>
                <i class="dot" v-if="item.isread === 0"></i>
              </div>
              <div class="info">{{ item.content }}</div>
            </div>
            <div class="time">
              <a-icon type="clock-circle" />&nbsp;<span>{{
                item.create_time
              }}</span>
            </div>
          </li>
          <md-scroll-view-more slot="more" :is-finished="isFinished">
          </md-scroll-view-more>
        </md-scroll-view>
      </ul>
      <div class="form-wrapper" v-else>
        <md-scroll-view
          ref="scrollView"
          :scrolling-x="false"
          :auto-reflow="true"
          @endReached="$_onEndReached"
          @refreshing="refresh"
        >
          <div class="empty" v-if="data.length === 0">
            <img src="@/assets/img/empty.png" alt="" />
          </div>
          <md-scroll-view-refresh
            slot="refresh"
            slot-scope="{ scrollTop, isRefreshActive, isRefreshing }"
            :scroll-top="scrollTop"
            :is-refreshing="isRefreshing"
            roller-color="#FD7400"
            :is-refresh-active="isRefreshActive"
          ></md-scroll-view-refresh>
          <new-card
            v-for="(item, index) in data"
            :cardInfo="item"
            :key="index"
          ></new-card>
          <md-scroll-view-more slot="more" :is-finished="isFinished">
          </md-scroll-view-more>
        </md-scroll-view>
      </div>
    </div>
  </hry-header>
</template>

<script>
import "@/assets/fonticon/iconfont.css";
import {
  ScrollView,
  ScrollViewMore,
  TabBar,
  ScrollViewRefresh
} from "mand-mobile";
export default {
  name: "index",
  components: {
    [ScrollView.name]: ScrollView,
    [ScrollViewMore.name]: ScrollViewMore,
    [ScrollViewRefresh.name]: ScrollViewRefresh,
    [TabBar.name]: TabBar,
    newCard: () => import("@/components/newCard/mobileIndex"),
    hryHeader: () => import("@/components/contentWrapper/index")
  },
  data() {
    return {
      isFinished: false,
      current: 0,
      items: [{ name: 0, label: "消息通知" }, { name: 1, label: "平台公告" }],
      data: [],
      curpage: 1,
      totalPage: 1
    };
  },
  methods: {
    $_onEndReached() {
      if (this.isFinished) {
        return;
      }
      this.curpage++;
      if (this.current === 0) {
        this.getInfo();
      } else {
        this.getAffiche();
      }
    },
    changeRead(id, index) {
      this.$ajax
        .post(
          this.$api.getMessage,
          {
            id: id
          },
          true
        )
        .then(() => {
          this.data[index].isread = 1;
        })
        .catch(err => {
          this.$message.error(err);
        });
    },
    changeTab(item) {
      this.$nextTick(() => {
        this.curpage = 1;
        this.isFinished = false;
        if (item.name === 0) {
          this.getInfo(true);
        } else {
          this.getAffiche(true);
        }
      });
    },
    refresh() {
      this.curpage = 1;
      this.isFinished = false;
      if (this.current === 0) {
        this.getInfo(true);
      } else {
        this.getAffiche(true);
      }
    },
    getAffiche(refresh) {
      this.$ajax
        .post(this.$api.getAffiche, {
          curpage: this.curpage
        })
        .then(res => {
          if (refresh) {
            this.data = res.data.list;
            this.$refs.scrollView.finishRefresh();
          } else {
            this.data.push.apply(this.data, res.data.list);
          }
          this.totalPage = res.data.total_page;
          if (this.curpage === this.totalPage) {
            this.isFinished = true;
          }
          this.$refs.scrollView.finishLoadMore();
        })
        .catch(err => {
          this.$message.error(err);
        });
    },
    getInfo(refresh) {
      this.$ajax
        .post(this.$api.getMessage, {
          curpage: this.curpage
        })
        .then(res => {
          if (refresh) {
            this.data = res.data.list;
            this.$refs.scrollViewOne.finishRefresh();
          } else {
            this.data.push.apply(this.data, res.data.list);
          }
          this.totalPage = res.data.total_page;
          if (this.curpage === this.totalPage) {
            this.isFinished = true;
          }
          this.$refs.scrollViewOne.finishLoadMore();
        })
        .catch(err => {
          this.$message.error(err);
        });
    }
  },
  created() {
    this.getInfo();
  }
};
</script>

<style scoped lang="less">
.personProject {
  height: 100%;
  .form-wrapper {
    height: calc(100% - 35px);
    width: 100%;
    padding: 0 40px;
    .link {
      font-size: 25px;
    }
    .ant-btn {
      margin-top: 117px;
      height: 75px;
      font-size: 30px;
    }
    .add {
      color: @primary-color;
      &:before {
        content: "+";
      }
    }
    .cut {
      &:before {
        content: "-";
      }
    }
    .type {
      display: inline-block;
      margin-top: 18px;
    }
  }
  li {
    padding: 25px 0 14px 0;
    border-bottom: 1px solid #e7e7ea;
  }
  .content {
    .flex_content();
    .message {
      position: relative;
      .iconxiaoxi {
        font-size: 39px;
        color: @primary-color;
      }
      .dot {
        position: absolute;
        top: 2px;
        right: 2px;
        display: inline-block;
        width: 10px;
        height: 10px;
        border-radius: 100%;
        background: #f5222d;
      }
    }
    .info {
      font-size: 27px;
      color: #000000;
      line-height: 50px;
      margin-left: 18px;
      margin-bottom: 14px;
      .flex_one();
    }
  }
  .time {
    text-align: right;
    color: #c2c2c2;
    font-size: 20px;
  }
}
</style>
